package model;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * A class representing characteristics of the hunter.
 * 
 * @author Martin Kromkamp and Albert-Jan Boer.
 * @version 2009.01.28
 */
public class Hunter implements Actor {
	 
    // A shared random number generator to control breeding.
    private static final Random rand = Randomizer.getRandom();
    // The field.
    private Field field;
    // The position in the field.
    private Location location;	
    // Values
    private Values value;
    
	public Hunter(boolean randomAge, Field field, Location location, Values value) {
		this.field = field;
        setLocation(location);
		this.value = value;
	}
	
	/**
     * Place the hunter at the new location in the given field.
     * @param newLocation The hunter's new location.
     */
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
	
    /**
     * Tell the hunter to look for foxes, rabbits or bears adjacent to its current location.
     * Only the first live fox, rabbit or bear is eaten.
     * @param location Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
     */
    private Location findFood(Location location)
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object animal = field.getObjectAt(where);
            if(animal instanceof Bear) {
                Bear bear = (Bear) animal;
                if(bear.isAlive()) { 
                    bear.setDead();
                    // Remove the dead fox from the field.
                    return where;
                }
            }
            if(animal instanceof Fox) {
                Fox fox = (Fox) animal;
                if(fox.isAlive()) { 
                    fox.setDead();
                    // Remove the dead fox from the field.
                    return where;
                }
            }
            if(animal instanceof Rabbit) {
                Rabbit rabbit = (Rabbit) animal;
                if(rabbit.isAlive()) { 
                    rabbit.setDead();
                    // Remove the dead fox from the field.
                    return where;
                }
            }
        }
        return null;
    }

    /**
     * Get the field.
     * @return field.
     */
    public Field getField()
    {
        return field;
    }
    
    /**
     * Get the current location.
     * @return the location of the hunter.
     */
    public Location getLocation()
    {
        return location;
    }
    
    /**
     * This is what the hunter does most of the time: it hunts for
     * foxes, rabbits, bears. 
     */
    public void act(List<Animal> newAnimal)
    {
        // Move towards a source of food if found.
        Location location = getLocation();
        Location newLocation = findFood(location);
        if(newLocation == null) { 
           // No food found - try to move to a free location.
           newLocation = getField().freeAdjacentLocation(location);
        }
        
        if(newLocation != null) {
           setLocation(newLocation);
        }
    }
}
